package adapter;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.core.BitmapSize;
import com.wenxin.administrator.zhufengfm.R;

import java.lang.reflect.Field;
import java.security.PrivateKey;
import java.util.List;

import entity.DiscoverRecommend.AlbumRecommend;
import entity.DiscoverRecommend.DiscoverRecommendAlbums;
import entity.DiscoverRecommend.DiscoverRecommendColumns;
import entity.DiscoverRecommend.DiscoverRecommendItem;
import entity.DiscoverRecommend.DiscoverRecommendSpecial;
import entity.DiscoverRecommend.SpecialItem;
import widgets.SpecialItemView;

/**
 * Created by Administrator on 15-10-21.
 */

/**
 * 处理所有发现Fragment的适配器
 */
public class DiscoverRecommendAdapter extends BaseAdapter {
    private Context context;
    private List<DiscoverRecommendItem> list;
    //使用xUtils工具类加载图片
    private BitmapUtils bitmapUtils;
    private BitmapDisplayConfig config;

    /**
     * 定义一个set的点击事件，用来被调用着设置点击事件
     */
     private View.OnClickListener onRecommendAlbumClickListener;
    public void setOnRecommendAlbumClickListener(View.OnClickListener onRecommendAlbumClickListener) {
        this.onRecommendAlbumClickListener = onRecommendAlbumClickListener;
    }

    /**
     * 适配器的构造函数
     * @param context
     * @param list
     */
    public DiscoverRecommendAdapter(Context context, List<DiscoverRecommendItem> list) {
        this.context = context;
        this.list = list;
        bitmapUtils=new BitmapUtils(context);
        initBitmapUtils();
    }

    /**
     * 加载图片的初始化
     */
    public void initBitmapUtils() {
        //设置加载时的照片
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.tab1);
        //设置默认的加载失败的图片
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.abc_ab_share_pack_mtrl_alpha);
        //设置默认的图片尺寸
        bitmapUtils.configDefaultBitmapMaxSize(new BitmapSize(100, 50));
        bitmapUtils.configDefaultConnectTimeout(10000);

        config = new BitmapDisplayConfig();
        //设置加载中的图片
        Resources resources = context.getResources();
        config.setLoadFailedDrawable(resources.getDrawable(R.drawable.tab2));
        config.setLoadingDrawable(resources.getDrawable(android.R.drawable.btn_default));
        config.setBitmapConfig(Bitmap.Config.RGB_565);
    }

    @Override
    public int getCount() {
        int count=0;
        if(list!=null)
        {
            count=list.size();
        }
        return count;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View ret=null;
        DiscoverRecommendItem item = list.get(i);
        if(item instanceof DiscoverRecommendAlbums){
            //发现新奇
            ret=bindAlbumsView(i,view,viewGroup);
        }else if(item instanceof DiscoverRecommendColumns){
            //精品听单
            ret=bindColumnView(i,view,viewGroup);
        }else if(item instanceof DiscoverRecommendSpecial){
            ret=bindSpecialView(i, view, viewGroup);
        }
        return ret;
    }

    /**
     *  利用反射来获取指定的R.id.xxx代表的view
     * @param container
     * @param fileName
     * @return
     */
    private static View findView(View container,String fileName){
        View  ret=null;
        if(container!=null&&fileName!=null){
           Class<R.id> idClass=R.id.class;
            try {
                Field field=idClass.getDeclaredField(fileName);
                int id=field.getInt(idClass);
                //通过静态常量获取整形int值来查找view
                ret=container.findViewById(id);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return ret;
    }

    /**
     * 小编推荐对应的getVIew返回的视图UI
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindAlbumsView(int position,View convertView,ViewGroup parent){
        View ret=null;
        if(convertView!=null) {
            ret=convertView;
        }else {
            LayoutInflater inflater = LayoutInflater.from(context);
            //获取小编推荐的UI视图
            ret = inflater.inflate(R.layout.discover_recommend_album_item
                    , parent, false);
            AlbumViewHolder holder = (AlbumViewHolder) ret.getTag();

            if (holder == null) {
                holder = new AlbumViewHolder();
                holder.txtTitle = (TextView) ret.findViewById(R.id.recommend_album_title);
                holder.txtMore = (TextView) ret.findViewById(R.id.recommend_album_more);
                //todo 点击more 进入推荐列表
                holder.txtMore.setOnClickListener(onRecommendAlbumClickListener);
                holder.albumIcons = new ImageView[3];
                holder.albumNames = new TextView[3];
                holder.trackNames = new TextView[3];
                for (int i = 0; i < 3; i++) {
                    //给holder设置
                    holder.albumIcons[i] = (ImageView) findView(ret, "recommend_album_icon_" + i);
                    //点击编辑 进入详情信息
                    holder.albumIcons[i].setOnClickListener(onRecommendAlbumClickListener);
                    holder.albumNames[i] = (TextView) findView(ret, "recommend_album_name_" + i);
                    holder.trackNames[i] = (TextView) findView(ret, "recommend_album_title_" + i);
                }
                ret.setTag(holder);
            }
            //3获取当前的小编推荐对象
            DiscoverRecommendAlbums albums = (DiscoverRecommendAlbums) list.get(position);
            //获取小标推荐标题
            String title = albums.getTitle();

            //设置标题
            holder.txtTitle.setText(title);
            holder.txtMore.setTag(position);
            //如果更多为空和不显示 不为空不显示
            if(!albums.isHasMore()){
                holder.txtMore.setVisibility(View.INVISIBLE);
            }else{
                holder.txtMore.setVisibility(View.VISIBLE);
            }

            //获取小编推荐里面的集合
            List<AlbumRecommend> albumList=albums.getList();
            int len=albumList.size();
            for (int i = 0; i <len ; i++) {
                //获取小编推荐中的一条数据
                AlbumRecommend alreCommend=albumList.get(i);
                //设置标题
                holder.albumNames[i].setText(alreCommend.getTitle());
                //设置子标题
                holder.trackNames[i].setText(alreCommend.getTrackTitle());
                //设置图片
                holder.albumIcons[i].setTag("albumRecommend"+position+":"+i);
                //将图片显示出来
                bitmapUtils.display(holder.albumIcons[i],alreCommend.getCoverLarge(),config);
                //小编推荐点击时候需要添加tag用来区分点击的哪一个
                String tag=position+":"+i;
                holder.albumIcons[i].setTag(tag);
            }

        }
        return ret;
    }

    /**
     * 发现新奇对应的getview
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindColumnView(int position,View convertView,ViewGroup parent){
        return null;
    }
    /**
     * 精品听单对应的getview
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindSpecialView(int position,View convertView,ViewGroup parent){
        View ret=null;
        if(convertView!=null){
            ret=convertView;
        }else {
            LayoutInflater inflater = LayoutInflater.from(context);
            //获取精品听单对应的UI视图
            ret = inflater.inflate(R.layout.discover_recommend_special_items
                    , parent, false);
            SpecialViewHolder holder = (SpecialViewHolder) ret.getTag();
            if(holder==null) {
                holder = new SpecialViewHolder();
                holder.txtTitle = (TextView) ret.findViewById(R.id.recommend_special_title);
                holder.txtMore = (TextView) ret.findViewById(R.id.recommend_special_more);
                //点击更多添加事件
                holder.txtMore.setOnClickListener(onRecommendAlbumClickListener);
                holder.itemContainer = (ListView) ret.findViewById(R.id.recommend_special_coninter);
                ret.setTag(holder);
            }
            //获取精品听单的对象
            DiscoverRecommendSpecial special = (DiscoverRecommendSpecial) list.get(position);
            //获取精品听单的标题
            holder.txtTitle.setText(special.getTitle());
            if (special.isHasMore()) {
                holder.txtMore.setVisibility(View.VISIBLE);
            } else {
                holder.txtMore.setVisibility(View.INVISIBLE);
            }
            // 将发现新奇里面的listview清除，因为里面有多个listviewitem
            holder.itemContainer.removeAllViews();
            //获取精品听单里面的集合
            List<SpecialItem> specialItems = special.getSpItem();
            if(specialItems!=null){
                int index=1;
                int len=specialItems.size();
                for(SpecialItem sp:specialItems){
                    index++;
                    //获取自定义listview
                    SpecialItemView itemView=new SpecialItemView(context);
                    //设置属性
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );
                    itemView.setLayoutParams(layoutParams);
                    holder.itemContainer.addView(itemView);
                    itemView.setTxtTitle(sp.getTitle());
                    itemView.setTxtSubTitle(sp.getSubtitle());
                    itemView.setNumber(sp.getFootnote());
                    //判断是否是最后一个：
                    itemView.showBottomLine(true);
                    String coverpath=sp.getCoverPath();
                    // todo  设置图片到自定义控件中
                    itemView.setImageBitmap(bitmapUtils,coverpath,config);
                    if(index ==  len-1) {
                        //最后一个
                        itemView.showBottomLine(false);
                    }

                }
            }
        }
        return ret;
    }

    /**
     * 小编推荐的viewholder
     */
    private static class AlbumViewHolder{
        public TextView txtTitle;
        public TextView txtMore;
        public ImageView[] albumIcons;//三张图片
        public TextView[] albumNames;//三张专辑标题
        public TextView[] trackNames;//三张曲目名称
    }

    /**
     * 精品听单的viewholder
     */
    private static class SpecialViewHolder{
        public TextView txtTitle;
        public TextView txtMore;
        public ListView itemContainer;
    }
    @Override
    public int getItemViewType(int position) {
        int ret=-1;
        DiscoverRecommendItem item = list.get(position);
        if(item instanceof DiscoverRecommendAlbums){
            ret=2;
        }else if(item instanceof DiscoverRecommendColumns){
            ret=1;
        }else if(item instanceof DiscoverRecommendSpecial){
            ret=0;
        }
        return ret;
    }

    //有小编推荐，精品听单，发现新奇
    public int getViewTypeCount() {
        return 3;
    }
}
